package sweet.helper

object Rand {
  val LETTER_CAP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  val LETTER_LOW = "abcdefghijklmnopqrstuvwxyz"
  val ALPHA = LETTER_CAP+LETTER_LOW
  val NUM = "0123456789"
  val ALPHA_NUM = ALPHA+NUM
  val SYMBOLS = "!@#$%^&*"
  val LETTERS_VOWELS = "AEIOU"
  val LETTERS_CONSONANTS = "BCDFGHJKLMNPQRSTVWXYZ"
  
  private val rand = new java.util.Random
  /** a random number between Integer.MIN_VALUE and Integer.MAX_VALUE*/
  def randInt = rand.nextInt
  /** a random number from 0 to max-1 inclusively*/
  def randInt(max: Int) = rand.nextInt(max)
  /** a random number from min to max-1 inclusively */
  def randInt(min: Int, max: Int) = min + rand.nextInt(max-min)
  
  /** Leading zero will not return, and if digit=1, you will not get zero! */
  def randDigitInt(digit: Int) = {
    //Int supports 10 digit, but only up to 2, so just chop at 9. use Long instead.
    assume(digit>0 && digit < 10, "Digit must be >0 && < 10") 
    val first = List(randInt(1, 10)) // can't be zero
    val rest = for(i<- 1 to digit-1) yield randInt(10)
    (first++rest).mkString.toInt
  }
  /** Leading zero will not return, and if digit=1, you will not get zero! */
  def randDigitLong(digit: Int) = {
    assume(digit>0 && digit < 19, "Digit must be >0 && < 19")
    val first = List(randInt(1, 10)) // can't be zero
    val rest = for(i<- 1 to digit-1) yield randInt(10)
    (first++rest).mkString.toLong
  }
  
  def randAlpha(len: Int) = (for(i<- 1 to len) yield ALPHA(randInt(ALPHA.length))).mkString
  def randAlphaNum(len: Int) = (for(i<- 1 to len) yield ALPHA_NUM(randInt(ALPHA_NUM.length))).mkString
  def randLetter(len: Int) = (for(i<- 1 to len) yield LETTER_CAP(randInt(LETTER_CAP.length))).mkString
  def randNum(len: Int) = (for(i<- 1 to len) yield NUM(randInt(NUM.length))).mkString
  def randSymbols(len: Int) = (for(i<- 1 to len) yield SYMBOLS(randInt(SYMBOLS.length))).mkString
  def randVowels(len: Int) = (for(i<- 1 to len) yield SYMBOLS(randInt(LETTERS_VOWELS.length))).mkString
  def randConsonants(len: Int) = (for(i<- 1 to len) yield SYMBOLS(randInt(LETTERS_CONSONANTS.length))).mkString
  
  def repeat(n: Int)(func: =>Unit) = for(i<- 1 to n) func
  def repeatWithIndex(n: Int)(func: Int=>Unit) = for(i<- 1 to n) func(i)
}
